package cn.shiwei.hr.service.impl;

import cn.shiwei.hr.constants.BaseConstants;
import cn.shiwei.hr.domain.Login;
import cn.shiwei.hr.httputil.HttpUtil;
import cn.shiwei.hr.mapper.LoginMapper;
import cn.shiwei.hr.repository.AssertUtil;
import cn.shiwei.hr.service.ILoginService;
import cn.shiwei.hr.service.IMealService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.prefs.BackingStoreException;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author 1363732197@qq.com
 * @since 2021-11-22
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {

    @Autowired
    private IMealService mealService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Long saveLoginAndMeal(Login login, Long mealId) {
        // 初始化login
        login.setEnabled(true);
        login.setAccountNonExpired(true);
        login.setAccountNonLocked(true);
        login.setCredentialsNonExpired(true);
        // TODO 密码加密
        login.setPassword(passwordEncoder.encode(login.getPassword()));

        // 保存login
        baseMapper.insert(login);
        Long loginId = login.getId();
        if (mealId != null) {
            // 保存套餐
            mealService.saveRelationWithLogin(loginId, mealId);
        }
        return loginId;
    }


    // 读取配置文件中信息
    @Value("${auth.password.uri}")
    private String getTokenUri;

    @Value("${auth.password.admin-client-id}")
    private String adminClientId;

    @Value("${auth.password.admin-client-secret}")
    private String adminClientSecret;

    @Value("${auth.password.user-client-id}")
    private String userClientId;

    @Value("${auth.password.user-client-secret}")
    private String userClientSecret;

    @Override
    public  Map<String, String> userLogin(Login login) {
        // 登录校验
        AssertUtil.stringIsEmpty(login.getUsername(), "用户名不能为空");
        AssertUtil.stringIsEmpty(login.getPassword(), "密码不能为空");

        // 校验用户是否存在
        Wrapper<Login> wrapper = new EntityWrapper<Login>();
        wrapper.eq("username", login.getUsername());
        Login loginFromDB = super.selectOne(wrapper);
        AssertUtil.isNull(loginFromDB, "用户不存在");

        String uri = null;
        if (loginFromDB.getType() == BaseConstants.Login.TYPE_ACCOUNT_BACKSTAGE) {
            // 组装url, 向security发送请求
            uri = String.format(getTokenUri, adminClientId, adminClientSecret, login.getUsername(), login.getPassword());
        } else {
            uri = String.format(getTokenUri, userClientId, userClientSecret, login.getUsername(), login.getPassword());
        }

        Map<String, String> resultMap= HttpUtil.sendPost(uri);

        return resultMap;
    }

    @Override
    public boolean insert(Login entity) {
        entity.setPassword(passwordEncoder.encode(entity.getPassword()));
        return super.insert(entity);
    }
}
